{
 "metadata": {
  "name": "",
  "signature": "sha256:ee839a37538f481abf14fdebcd8219c687df840e35bf9d9437399180d9b23a7f"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Advanced NumPy"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import division\n",
      "from numpy.random import randn\n",
      "from pandas import Series\n",
      "import numpy as np\n",
      "np.set_printoptions(precision=4)\n",
      "import sys; sys.path.append('book_scripts')\n",
      "%cd book_scripts"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "/home/phillip/Documents/code/py/pandas-book/rev_539000/book_scripts\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "ndarray object internals"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "NumPy dtype hierarchy"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ints = np.ones(10, dtype=np.uint16)\n",
      "floats = np.ones(10, dtype=np.float32)\n",
      "np.issubdtype(ints.dtype, np.integer)\n",
      "np.issubdtype(floats.dtype, np.floating)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 2,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.float64.mro()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "[numpy.float64,\n",
        " numpy.floating,\n",
        " numpy.inexact,\n",
        " numpy.number,\n",
        " numpy.generic,\n",
        " float,\n",
        " object]"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Advanced array manipulation"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Reshaping arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(8)\n",
      "arr\n",
      "arr.reshape((4, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "array([[0, 1],\n",
        "       [2, 3],\n",
        "       [4, 5],\n",
        "       [6, 7]])"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.reshape((4, 2)).reshape((2, 4))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 5,
       "text": [
        "array([[0, 1, 2, 3],\n",
        "       [4, 5, 6, 7]])"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(15)\n",
      "arr.reshape((5, -1))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "array([[ 0,  1,  2],\n",
        "       [ 3,  4,  5],\n",
        "       [ 6,  7,  8],\n",
        "       [ 9, 10, 11],\n",
        "       [12, 13, 14]])"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "other_arr = np.ones((3, 5))\n",
      "other_arr.shape\n",
      "arr.reshape(other_arr.shape)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 7,
       "text": [
        "array([[ 0,  1,  2,  3,  4],\n",
        "       [ 5,  6,  7,  8,  9],\n",
        "       [10, 11, 12, 13, 14]])"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(15).reshape((5, 3))\n",
      "arr\n",
      "arr.ravel()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.flatten()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 9,
       "text": [
        "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "C vs. Fortran order"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(12).reshape((3, 4))\n",
      "arr\n",
      "arr.ravel()\n",
      "arr.ravel('F')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 10,
       "text": [
        "array([ 0,  4,  8,  1,  5,  9,  2,  6, 10,  3,  7, 11])"
       ]
      }
     ],
     "prompt_number": 10
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Concatenating and splitting arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
      "arr2 = np.array([[7, 8, 9], [10, 11, 12]])\n",
      "np.concatenate([arr1, arr2], axis=0)\n",
      "np.concatenate([arr1, arr2], axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 11,
       "text": [
        "array([[ 1,  2,  3,  7,  8,  9],\n",
        "       [ 4,  5,  6, 10, 11, 12]])"
       ]
      }
     ],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.vstack((arr1, arr2))\n",
      "np.hstack((arr1, arr2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 12,
       "text": [
        "array([[ 1,  2,  3,  7,  8,  9],\n",
        "       [ 4,  5,  6, 10, 11, 12]])"
       ]
      }
     ],
     "prompt_number": 12
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from numpy.random import randn\n",
      "arr = randn(5, 2)\n",
      "arr\n",
      "first, second, third = np.split(arr, [1, 3])\n",
      "first\n",
      "second\n",
      "third"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 13,
       "text": [
        "array([[ 0.0865, -0.0964],\n",
        "       [ 1.7154,  0.3276]])"
       ]
      }
     ],
     "prompt_number": 13
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Stacking helpers: "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(6)\n",
      "arr1 = arr.reshape((3, 2))\n",
      "arr2 = randn(3, 2)\n",
      "np.r_[arr1, arr2]\n",
      "np.c_[np.r_[arr1, arr2], arr]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 14,
       "text": [
        "array([[ 0.    ,  1.    ,  0.    ],\n",
        "       [ 2.    ,  3.    ,  1.    ],\n",
        "       [ 4.    ,  5.    ,  2.    ],\n",
        "       [-0.477 ,  1.153 ,  3.    ],\n",
        "       [ 0.0919, -0.3852,  4.    ],\n",
        "       [-1.891 , -1.4744,  5.    ]])"
       ]
      }
     ],
     "prompt_number": 14
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.c_[1:6, -10:-5]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 15,
       "text": [
        "array([[  1, -10],\n",
        "       [  2,  -9],\n",
        "       [  3,  -8],\n",
        "       [  4,  -7],\n",
        "       [  5,  -6]])"
       ]
      }
     ],
     "prompt_number": 15
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Repeating elements: tile and repeat"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(3)\n",
      "arr.repeat(3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 16,
       "text": [
        "array([0, 0, 0, 1, 1, 1, 2, 2, 2])"
       ]
      }
     ],
     "prompt_number": 16
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.repeat([2, 3, 4])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 17,
       "text": [
        "array([0, 0, 1, 1, 1, 2, 2, 2, 2])"
       ]
      }
     ],
     "prompt_number": 17
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(2, 2)\n",
      "arr\n",
      "arr.repeat(2, axis=0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 18,
       "text": [
        "array([[ 0.8373, -0.0382],\n",
        "       [ 0.8373, -0.0382],\n",
        "       [-2.3026, -3.1157],\n",
        "       [-2.3026, -3.1157]])"
       ]
      }
     ],
     "prompt_number": 18
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.repeat([2, 3], axis=0)\n",
      "arr.repeat([2, 3], axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 19,
       "text": [
        "array([[ 0.8373,  0.8373, -0.0382, -0.0382, -0.0382],\n",
        "       [-2.3026, -2.3026, -3.1157, -3.1157, -3.1157]])"
       ]
      }
     ],
     "prompt_number": 19
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr\n",
      "np.tile(arr, 2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 20,
       "text": [
        "array([[ 0.8373, -0.0382,  0.8373, -0.0382],\n",
        "       [-2.3026, -3.1157, -2.3026, -3.1157]])"
       ]
      }
     ],
     "prompt_number": 20
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr\n",
      "np.tile(arr, (2, 1))\n",
      "np.tile(arr, (3, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 21,
       "text": [
        "array([[ 0.8373, -0.0382,  0.8373, -0.0382],\n",
        "       [-2.3026, -3.1157, -2.3026, -3.1157],\n",
        "       [ 0.8373, -0.0382,  0.8373, -0.0382],\n",
        "       [-2.3026, -3.1157, -2.3026, -3.1157],\n",
        "       [ 0.8373, -0.0382,  0.8373, -0.0382],\n",
        "       [-2.3026, -3.1157, -2.3026, -3.1157]])"
       ]
      }
     ],
     "prompt_number": 21
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Fancy indexing equivalents: take and put"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10) * 100\n",
      "inds = [7, 1, 2, 6]\n",
      "arr[inds]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 22,
       "text": [
        "array([700, 100, 200, 600])"
       ]
      }
     ],
     "prompt_number": 22
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.take(inds)\n",
      "arr.put(inds, 42)\n",
      "arr\n",
      "arr.put(inds, [40, 41, 42, 43])\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 23,
       "text": [
        "array([  0,  41,  42, 300, 400, 500,  43,  40, 800, 900])"
       ]
      }
     ],
     "prompt_number": 23
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "inds = [2, 0, 2, 1]\n",
      "arr = randn(2, 4)\n",
      "arr\n",
      "arr.take(inds, axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 24,
       "text": [
        "array([[ 0.7526, -0.5752,  0.7526, -0.9173],\n",
        "       [ 0.5017,  0.8759,  0.5017, -0.4772]])"
       ]
      }
     ],
     "prompt_number": 24
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Broadcasting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(5)\n",
      "arr\n",
      "arr * 4"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 25,
       "text": [
        "array([ 0,  4,  8, 12, 16])"
       ]
      }
     ],
     "prompt_number": 25
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(4, 3)\n",
      "arr.mean(0)\n",
      "demeaned = arr - arr.mean(0)\n",
      "demeaned\n",
      "demeaned.mean(0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 26,
       "text": [
        "array([ 0.,  0., -0.])"
       ]
      }
     ],
     "prompt_number": 26
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr\n",
      "row_means = arr.mean(1)\n",
      "row_means.reshape((4, 1))\n",
      "demeaned = arr - row_means.reshape((4, 1))\n",
      "demeaned.mean(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 27,
       "text": [
        "array([ 0., -0.,  0., -0.])"
       ]
      }
     ],
     "prompt_number": 27
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Broadcasting over other axes"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr - arr.mean(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "ValueError",
       "evalue": "operands could not be broadcast together with shapes (4,3) (4,) ",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-28-7b87b85a20b2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0marr\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[1;31mValueError\u001b[0m: operands could not be broadcast together with shapes (4,3) (4,) "
       ]
      }
     ],
     "prompt_number": 28
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr - arr.mean(1).reshape((4, 1))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 29,
       "text": [
        "array([[-1.6838, -0.3736,  2.0574],\n",
        "       [ 1.513 , -0.6511, -0.8619],\n",
        "       [ 0.4619,  0.3708, -0.8327],\n",
        "       [ 0.0346, -0.6972,  0.6626]])"
       ]
      }
     ],
     "prompt_number": 29
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.zeros((4, 4))\n",
      "arr_3d = arr[:, np.newaxis, :]\n",
      "arr_3d.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 30,
       "text": [
        "(4, 1, 4)"
       ]
      }
     ],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr_1d = np.random.normal(size=3)\n",
      "arr_1d[:, np.newaxis]\n",
      "arr_1d[np.newaxis, :]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 31,
       "text": [
        "array([[ 0.6034,  0.4693,  0.6303]])"
       ]
      }
     ],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(3, 4, 5)\n",
      "depth_means = arr.mean(2)\n",
      "depth_means\n",
      "demeaned = arr - depth_means[:, :, np.newaxis]\n",
      "demeaned.mean(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "array([[ 0.,  0., -0.,  0.],\n",
        "       [ 0.,  0.,  0., -0.],\n",
        "       [ 0., -0., -0., -0.]])"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def demean_axis(arr, axis=0):\n",
      "    means = arr.mean(axis)\n",
      "\n",
      "    # This generalized things like [:, :, np.newaxis] to N dimensions\n",
      "    indexer = [slice(None)] * arr.ndim\n",
      "    indexer[axis] = np.newaxis\n",
      "    return arr - means[indexer]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 34
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Setting array values by broadcasting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.zeros((4, 3))\n",
      "arr[:] = 5\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 35,
       "text": [
        "array([[ 5.,  5.,  5.],\n",
        "       [ 5.,  5.,  5.],\n",
        "       [ 5.,  5.,  5.],\n",
        "       [ 5.,  5.,  5.]])"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "col = np.array([1.28, -0.42, 0.44, 1.6])\n",
      "arr[:] = col[:, np.newaxis]\n",
      "arr\n",
      "arr[:2] = [[-1.37], [0.509]]\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 36,
       "text": [
        "array([[-1.37 , -1.37 , -1.37 ],\n",
        "       [ 0.509,  0.509,  0.509],\n",
        "       [ 0.44 ,  0.44 ,  0.44 ],\n",
        "       [ 1.6  ,  1.6  ,  1.6  ]])"
       ]
      }
     ],
     "prompt_number": 36
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Advanced ufunc usage"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Ufunc instance methods"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10)\n",
      "np.add.reduce(arr)\n",
      "arr.sum()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 37,
       "text": [
        "45"
       ]
      }
     ],
     "prompt_number": 37
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.random.seed(12346)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 38
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(5, 5)\n",
      "arr[::2].sort(1) # sort a few rows\n",
      "arr[:, :-1] < arr[:, 1:]\n",
      "np.logical_and.reduce(arr[:, :-1] < arr[:, 1:], axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 39,
       "text": [
        "array([ True, False,  True, False,  True], dtype=bool)"
       ]
      }
     ],
     "prompt_number": 39
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(15).reshape((3, 5))\n",
      "np.add.accumulate(arr, axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 40,
       "text": [
        "array([[ 0,  1,  3,  6, 10],\n",
        "       [ 5, 11, 18, 26, 35],\n",
        "       [10, 21, 33, 46, 60]])"
       ]
      }
     ],
     "prompt_number": 40
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(3).repeat([1, 2, 2])\n",
      "arr\n",
      "np.multiply.outer(arr, np.arange(5))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 41,
       "text": [
        "array([[0, 0, 0, 0, 0],\n",
        "       [0, 1, 2, 3, 4],\n",
        "       [0, 1, 2, 3, 4],\n",
        "       [0, 2, 4, 6, 8],\n",
        "       [0, 2, 4, 6, 8]])"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = np.subtract.outer(randn(3, 4), randn(5))\n",
      "result.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 42,
       "text": [
        "(3, 4, 5)"
       ]
      }
     ],
     "prompt_number": 42
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10)\n",
      "np.add.reduceat(arr, [0, 5, 8])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 43,
       "text": [
        "array([10, 18, 17])"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.multiply.outer(np.arange(4), np.arange(5))\n",
      "arr\n",
      "np.add.reduceat(arr, [0, 2, 4], axis=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 44,
       "text": [
        "array([[ 0,  0,  0],\n",
        "       [ 1,  5,  4],\n",
        "       [ 2, 10,  8],\n",
        "       [ 3, 15, 12]])"
       ]
      }
     ],
     "prompt_number": 44
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Custom ufuncs"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def add_elements(x, y):\n",
      "    return x + y\n",
      "add_them = np.frompyfunc(add_elements, 2, 1)\n",
      "add_them(np.arange(8), np.arange(8))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 45,
       "text": [
        "array([0, 2, 4, 6, 8, 10, 12, 14], dtype=object)"
       ]
      }
     ],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "add_them = np.vectorize(add_elements, otypes=[np.float64])\n",
      "add_them(np.arange(8), np.arange(8))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 46,
       "text": [
        "array([  0.,   2.,   4.,   6.,   8.,  10.,  12.,  14.])"
       ]
      }
     ],
     "prompt_number": 46
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(10000)\n",
      "%timeit add_them(arr, arr)\n",
      "%timeit np.add(arr, arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1000 loops, best of 3: 1.7 ms per loop\n",
        "100000 loops, best of 3: 4.68 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 47
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Structured and record arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dtype = [('x', np.float64), ('y', np.int32)]\n",
      "sarr = np.array([(1.5, 6), (np.pi, -2)], dtype=dtype)\n",
      "sarr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 48,
       "text": [
        "array([(1.5, 6), (3.141592653589793, -2)], \n",
        "      dtype=[('x', '<f8'), ('y', '<i4')])"
       ]
      }
     ],
     "prompt_number": 48
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sarr[0]\n",
      "sarr[0]['y']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 49,
       "text": [
        "6"
       ]
      }
     ],
     "prompt_number": 49
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sarr['x']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 50,
       "text": [
        "array([ 1.5   ,  3.1416])"
       ]
      }
     ],
     "prompt_number": 50
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Nested dtypes and multidimensional fields"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dtype = [('x', np.int64, 3), ('y', np.int32)]\n",
      "arr = np.zeros(4, dtype=dtype)\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 51,
       "text": [
        "array([([0, 0, 0], 0), ([0, 0, 0], 0), ([0, 0, 0], 0), ([0, 0, 0], 0)], \n",
        "      dtype=[('x', '<i8', (3,)), ('y', '<i4')])"
       ]
      }
     ],
     "prompt_number": 51
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[0]['x']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 52,
       "text": [
        "array([0, 0, 0])"
       ]
      }
     ],
     "prompt_number": 52
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr['x']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 53,
       "text": [
        "array([[0, 0, 0],\n",
        "       [0, 0, 0],\n",
        "       [0, 0, 0],\n",
        "       [0, 0, 0]])"
       ]
      }
     ],
     "prompt_number": 53
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "dtype = [('x', [('a', 'f8'), ('b', 'f4')]), ('y', np.int32)]\n",
      "data = np.array([((1, 2), 5), ((3, 4), 6)], dtype=dtype)\n",
      "data['x']\n",
      "data['y']\n",
      "data['x']['a']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 54,
       "text": [
        "array([ 1.,  3.])"
       ]
      }
     ],
     "prompt_number": 54
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Why use structured arrays?"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Structured array manipulations: numpy.lib.recfunctions"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "More about sorting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(6)\n",
      "arr.sort()\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 55,
       "text": [
        "array([-1.082 ,  0.3759,  0.8014,  1.1397,  1.2888,  1.8413])"
       ]
      }
     ],
     "prompt_number": 55
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(3, 5)\n",
      "arr\n",
      "arr[:, 0].sort()  # Sort first column values in-place\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 56,
       "text": [
        "array([[-1.0111, -1.4711,  0.8705, -0.0847, -1.1329],\n",
        "       [-0.3318, -0.3436,  2.1714,  0.1234, -0.0189],\n",
        "       [ 0.1773,  0.7424,  0.8548,  1.038 , -0.329 ]])"
       ]
      }
     ],
     "prompt_number": 56
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(5)\n",
      "arr\n",
      "np.sort(arr)\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 57,
       "text": [
        "array([-1.1181, -0.2415, -2.0051,  0.7379, -1.0614])"
       ]
      }
     ],
     "prompt_number": 57
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(3, 5)\n",
      "arr\n",
      "arr.sort(axis=1)\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 58,
       "text": [
        "array([[-0.2682, -0.1872,  0.5955,  0.9111,  1.3389],\n",
        "       [-0.5168, -0.3215, -0.1989,  1.0054,  1.1925],\n",
        "       [-1.7638, -0.2222, -0.2171,  0.3969,  0.6071]])"
       ]
      }
     ],
     "prompt_number": 58
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[:, ::-1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 59,
       "text": [
        "array([[ 1.3389,  0.9111,  0.5955, -0.1872, -0.2682],\n",
        "       [ 1.1925,  1.0054, -0.1989, -0.3215, -0.5168],\n",
        "       [ 0.6071,  0.3969, -0.2171, -0.2222, -1.7638]])"
       ]
      }
     ],
     "prompt_number": 59
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Indirect sorts: argsort and lexsort"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "values = np.array([5, 0, 1, 3, 2])\n",
      "indexer = values.argsort()\n",
      "indexer\n",
      "values[indexer]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 60,
       "text": [
        "array([0, 1, 2, 3, 5])"
       ]
      }
     ],
     "prompt_number": 60
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(3, 5)\n",
      "arr[0] = values\n",
      "arr\n",
      "arr[:, arr[0].argsort()]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 61,
       "text": [
        "array([[ 0.    ,  1.    ,  2.    ,  3.    ,  5.    ],\n",
        "       [-0.1378,  2.1777,  0.8356, -0.4728, -0.3636],\n",
        "       [ 0.2316,  0.728 ,  1.9956, -1.3918, -0.2089]])"
       ]
      }
     ],
     "prompt_number": 61
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "first_name = np.array(['Bob', 'Jane', 'Steve', 'Bill', 'Barbara'])\n",
      "last_name = np.array(['Jones', 'Arnold', 'Arnold', 'Jones', 'Walters'])\n",
      "sorter = np.lexsort((first_name, last_name))\n",
      "zip(last_name[sorter], first_name[sorter])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 62,
       "text": [
        "[('Arnold', 'Jane'),\n",
        " ('Arnold', 'Steve'),\n",
        " ('Jones', 'Bill'),\n",
        " ('Jones', 'Bob'),\n",
        " ('Walters', 'Barbara')]"
       ]
      }
     ],
     "prompt_number": 62
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Alternate sort algorithms"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "values = np.array(['2:first', '2:second', '1:first', '1:second', '1:third'])\n",
      "key = np.array([2, 2, 1, 1, 1])\n",
      "indexer = key.argsort(kind='mergesort')\n",
      "indexer\n",
      "values.take(indexer)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 63,
       "text": [
        "array(['1:first', '1:second', '1:third', '2:first', '2:second'], \n",
        "      dtype='|S8')"
       ]
      }
     ],
     "prompt_number": 63
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "numpy.searchsorted: Finding elements in a sorted array"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([0, 1, 7, 12, 15])\n",
      "arr.searchsorted(9)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 64,
       "text": [
        "3"
       ]
      }
     ],
     "prompt_number": 64
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.searchsorted([0, 8, 11, 16])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 65,
       "text": [
        "array([0, 3, 3, 5])"
       ]
      }
     ],
     "prompt_number": 65
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([0, 0, 0, 1, 1, 1, 1])\n",
      "arr.searchsorted([0, 1])\n",
      "arr.searchsorted([0, 1], side='right')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 66,
       "text": [
        "array([3, 7])"
       ]
      }
     ],
     "prompt_number": 66
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = np.floor(np.random.uniform(0, 10000, size=50))\n",
      "bins = np.array([0, 100, 1000, 5000, 10000])\n",
      "data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 67,
       "text": [
        "array([ 8304.,  4181.,  9352.,  4907.,  3250.,  8546.,  2673.,  6152.,\n",
        "        2774.,  5130.,  9553.,  4997.,  1794.,  9688.,   426.,  1612.,\n",
        "         651.,  8653.,  1695.,  4764.,  1052.,  4836.,  8020.,  3479.,\n",
        "        1513.,  5872.,  8992.,  7656.,  4764.,  5383.,  2319.,  4280.,\n",
        "        4150.,  8601.,  3946.,  9904.,  7286.,  9969.,  6032.,  4574.,\n",
        "        8480.,  4298.,  2708.,  7358.,  6439.,  7916.,  3899.,  9182.,\n",
        "         871.,  7973.])"
       ]
      }
     ],
     "prompt_number": 67
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "labels = bins.searchsorted(data)\n",
      "labels"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 68,
       "text": [
        "array([4, 3, 4, 3, 3, 4, 3, 4, 3, 4, 4, 3, 3, 4, 2, 3, 2, 4, 3, 3, 3, 3, 4,\n",
        "       3, 3, 4, 4, 4, 3, 4, 3, 3, 3, 4, 3, 4, 4, 4, 4, 3, 4, 3, 3, 4, 4, 4,\n",
        "       3, 4, 2, 4])"
       ]
      }
     ],
     "prompt_number": 68
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Series(data).groupby(labels).mean()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 69,
       "text": [
        "2     649.3333\n",
        "3    3411.5217\n",
        "4    7935.0417\n",
        "dtype: float64"
       ]
      }
     ],
     "prompt_number": 69
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.digitize(data, bins)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 70,
       "text": [
        "array([4, 3, 4, 3, 3, 4, 3, 4, 3, 4, 4, 3, 3, 4, 2, 3, 2, 4, 3, 3, 3, 3, 4,\n",
        "       3, 3, 4, 4, 4, 3, 4, 3, 3, 3, 4, 3, 4, 4, 4, 4, 3, 4, 3, 3, 4, 4, 4,\n",
        "       3, 4, 2, 4])"
       ]
      }
     ],
     "prompt_number": 70
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "NumPy matrix class"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "X =  np.array([[ 8.82768214,  3.82222409, -1.14276475,  2.04411587],\n",
      "               [ 3.82222409,  6.75272284,  0.83909108,  2.08293758],\n",
      "               [-1.14276475,  0.83909108,  5.01690521,  0.79573241],\n",
      "               [ 2.04411587,  2.08293758,  0.79573241,  6.24095859]])\n",
      "X[:, 0]  # one-dimensional\n",
      "y = X[:, :1]  # two-dimensional by slicing\n",
      "X\n",
      "y"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 71,
       "text": [
        "array([[ 8.8277],\n",
        "       [ 3.8222],\n",
        "       [-1.1428],\n",
        "       [ 2.0441]])"
       ]
      }
     ],
     "prompt_number": 71
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.dot(y.T, np.dot(X, y))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 72,
       "text": [
        "array([[ 1195.468]])"
       ]
      }
     ],
     "prompt_number": 72
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Xm = np.matrix(X)\n",
      "ym = Xm[:, 0]\n",
      "Xm\n",
      "ym\n",
      "ym.T * Xm * ym"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 73,
       "text": [
        "matrix([[ 1195.468]])"
       ]
      }
     ],
     "prompt_number": 73
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Xm.I * X"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 74,
       "text": [
        "matrix([[ 1.,  0., -0.,  0.],\n",
        "        [-0.,  1.,  0., -0.],\n",
        "        [ 0.,  0.,  1.,  0.],\n",
        "        [ 0.,  0.,  0.,  1.]])"
       ]
      }
     ],
     "prompt_number": 74
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Advanced array input and output"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Memory-mapped files"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mmap = np.memmap('mymmap', dtype='float64', mode='w+', shape=(10000, 10000))\n",
      "mmap"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 75,
       "text": [
        "memmap([[ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
        "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
        "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
        "       ..., \n",
        "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
        "       [ 0.,  0.,  0., ...,  0.,  0.,  0.],\n",
        "       [ 0.,  0.,  0., ...,  0.,  0.,  0.]])"
       ]
      }
     ],
     "prompt_number": 75
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "section = mmap[:5]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 76
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "section[:] = np.random.randn(5, 10000)\n",
      "mmap.flush()\n",
      "mmap\n",
      "del mmap"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 77
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mmap = np.memmap('mymmap', dtype='float64', shape=(10000, 10000))\n",
      "mmap"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 78,
       "text": [
        "memmap([[-0.1614, -0.1768,  0.422 , ..., -0.2195, -0.1256, -0.4012],\n",
        "       [ 0.4898, -2.2219, -0.7684, ..., -2.3517, -1.0782,  1.3208],\n",
        "       [-0.6875,  1.6901, -0.7444, ..., -1.4218, -0.0509,  1.2224],\n",
        "       ..., \n",
        "       [ 0.    ,  0.    ,  0.    , ...,  0.    ,  0.    ,  0.    ],\n",
        "       [ 0.    ,  0.    ,  0.    , ...,  0.    ,  0.    ,  0.    ],\n",
        "       [ 0.    ,  0.    ,  0.    , ...,  0.    ,  0.    ,  0.    ]])"
       ]
      }
     ],
     "prompt_number": 78
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%xdel mmap\n",
      "!rm mymmap"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 79
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "HDF5 and other array storage options"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Performance tips"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "The importance of contiguous memory"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr_c = np.ones((1000, 1000), order='C')\n",
      "arr_f = np.ones((1000, 1000), order='F')\n",
      "arr_c.flags\n",
      "arr_f.flags\n",
      "arr_f.flags.f_contiguous"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 80,
       "text": [
        "True"
       ]
      }
     ],
     "prompt_number": 80
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%timeit arr_c.sum(1)\n",
      "%timeit arr_f.sum(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1000 loops, best of 3: 565 \u00b5s per loop\n",
        "1000 loops, best of 3: 597 \u00b5s per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 81
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr_f.copy('C').flags"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 82,
       "text": [
        "  C_CONTIGUOUS : True\n",
        "  F_CONTIGUOUS : False\n",
        "  OWNDATA : True\n",
        "  WRITEABLE : True\n",
        "  ALIGNED : True\n",
        "  UPDATEIFCOPY : False"
       ]
      }
     ],
     "prompt_number": 82
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr_c[:50].flags.contiguous\n",
      "arr_c[:, :50].flags"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 83,
       "text": [
        "  C_CONTIGUOUS : False\n",
        "  F_CONTIGUOUS : False\n",
        "  OWNDATA : False\n",
        "  WRITEABLE : True\n",
        "  ALIGNED : True\n",
        "  UPDATEIFCOPY : False"
       ]
      }
     ],
     "prompt_number": 83
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%xdel arr_c\n",
      "%xdel arr_f\n",
      "%cd .."
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "/home/phillip/Documents/code/py/pandas-book/rev_539000\n"
       ]
      }
     ],
     "prompt_number": 84
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Other speed options: Cython, f2py, C"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "```cython\n",
      "from numpy cimport ndarray, float64_t\n",
      "\n",
      "def sum_elements(ndarray[float64_t] arr):\n",
      "    cdef Py_ssize_t i, n = len(arr)\n",
      "    cdef float64_t result = 0\n",
      "\n",
      "    for i in range(n):\n",
      "        result += arr[i]\n",
      "\n",
      "    return result\n",
      "```"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}